//
//  LDShowAlertOrSheet.swift
//  DingDongZP
//
//  Created by 三秒文化 on 16/5/20.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

import UIKit
//sheet 辞去通知
let DISSMISSACTIONSHEET = "DISSMISSACTIONSHEET";

extension NSObject {
    
    
    //通知样式
    func showLDAlertView(info:String) {
        
        
        let alertVC  = PCLBlurEffectAlert.Controller(title: "提示",
                                                     message: info,
                                                     style: .AlertVertical)
        
        let action1 = PCLBlurEffectAlert.AlertAction(title: "我知道了",
                                                     style: .Default,
                                                     handler: { action in
                                                        
                                                        
        })
        
        
        alertVC.addAction(action1)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        //        alertVC.configure(backgroundColor: LDGolbalColor.BgGoundGray)
        //        alertVC.configure(backgroundColor: UIColor.whiteColor())
        
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Default:LDFontColor.Green], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        
        NSOperationQueue .mainQueue().addOperationWithBlock {
            
            let target = UIApplication.sharedApplication().keyWindow?.rootViewController
            target!.presentViewController(alertVC, animated: true, completion: nil)
        }
        
        
    }
    //通知样式
    func showLDAlertView(title:String,info:String) {
        
        
        let alertVC  = PCLBlurEffectAlert.Controller(title: title,
                                                     message: info,
                                                     style: .AlertVertical)
        
        let action1 = PCLBlurEffectAlert.AlertAction(title: "我知道了",
                                                     style: .Default,
                                                     handler: { action in
                                                        
                                                        
        })
        
        
        alertVC.addAction(action1)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDGolbalColor.Green)
        alertVC.configure(messageFont: LDFont.Font13, textColor: LDFontColor.DeepGray)
        //        alertVC.configure(backgroundColor: LDGolbalColor.BgGoundGray)
        //        alertVC.configure(backgroundColor: UIColor.whiteColor())
        
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Default:LDFontColor.Green], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        NSOperationQueue .mainQueue().addOperationWithBlock {
            
            let target = UIApplication.sharedApplication().keyWindow?.rootViewController
            target!.presentViewController(alertVC, animated: true, completion: nil)
        }
        
        
    }
    
    
    //确认 否 样式
    func showLDAlertView(info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        
        
        
        //        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        let alertVC = PCLBlurEffectAlert.Controller(title: info,
                                                    message: nil,
                                                    style: .Alert)
        let okAction = PCLBlurEffectAlert.AlertAction(title: "是",
                                                      style: .Default,
                                                      handler: { action in
                                                        
                                                        if sure != nil {
                                                            sure!()
                                                        }
                                                        
        })
        
        okAction.button.titleLabel?.textColor = LDFontColor.Green
        
        let cancelAction = PCLBlurEffectAlert.AlertAction(title: "否",
                                                          style: .Cancel,
                                                          handler: { action in
                                                            
                                                            if cancel != nil {
                                                                cancel!()
                                                            }
                                                            
        })
        
        
        
        alertVC.addAction(okAction)
        alertVC.addAction(cancelAction)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Default:LDFontColor.Green], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        //        NSOperationQueue.mainQueue().addOperationWithBlock {
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        target!.presentViewController(alertVC, animated: true, completion: nil)
        //        }
    }
    
    
    //确认 title message 样式
    func showLDAlertView(title:String,info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        let alertVC = PCLBlurEffectAlert.Controller(title: title,
                                                    message: info,
                                                    style: .Alert)
        let okAction = PCLBlurEffectAlert.AlertAction(title: "是",
                                                      style: .Default,
                                                      handler: { action in
                                                        
                                                        if sure != nil {
                                                            sure!()
                                                        }
                                                        
        })
        
        okAction.button.titleLabel?.textColor = LDFontColor.Green
        
        let cancelAction = PCLBlurEffectAlert.AlertAction(title: "否",
                                                          style: .Cancel,
                                                          handler: { action in
                                                            
                                                            if cancel != nil {
                                                                cancel!()
                                                            }
                                                            
        })
        
        
        
        alertVC.addAction(okAction)
        alertVC.addAction(cancelAction)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Default:LDFontColor.Green], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        target!.presentViewController(alertVC, animated: true, completion: nil)
        
    }
    
    
    func showLDDefaultAlertView(info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        let alertVC = UIAlertController.init(title: nil, message: info, preferredStyle: .Alert)
        
        let okaction = UIAlertAction.init(title: "是", style: UIAlertActionStyle.Default) { (action) in
            
            if sure != nil {
                
                sure!()
            }
            
        }
        
        let cancelaction = UIAlertAction.init(title: "否", style: .Cancel) { (action) in
            
            if cancel != nil {
                
                cancel!()
            }
            
        }
        
        alertVC.addAction(okaction)
        alertVC.addAction(cancelaction)
        
        NSOperationQueue .mainQueue().addOperationWithBlock {
            
            let target = UIApplication.sharedApplication().keyWindow?.rootViewController
            target!.presentViewController(alertVC, animated: true, completion: nil)
        }
        
    }
    //    
    //    //确认 否 样式
    //    func showLDAlertView(target:UIViewController,info:String,sure:(()->Void)?,cancel:(()->Void)?) {
    //            
    //            //            let target = LDMeTabBarVC
    //            let alertVC = PCLBlurEffectAlert.Controller(title: info,
    //                message: nil,
    //                style: .Alert)
    //            let okAction = PCLBlurEffectAlert.AlertAction(title: "是",
    //                style: .Destructive,
    //                handler: { action in
    //                    
    //                    if sure != nil {
    //                        sure!()
    //                    }
    //                    
    //            })
    //            
    //            
    //            let cancelAction = PCLBlurEffectAlert.AlertAction(title: "否",
    //                style: .Cancel,
    //                handler: { action in
    //                    
    //                    if cancel != nil {
    //                        cancel!()
    //                    }
    //                    
    //            })
    //            
    //            
    //            alertVC.addAction(okAction)
    //            alertVC.addAction(cancelAction)
    //            target.presentViewController(alertVC, animated: true, completion: nil)
    //        
    //    }
    
    
    //action Sheet 选择列表样式
    func showLDActionSheetView(title:String?,labHeight:AnyObject?,
                               labsTitleArr:Array<String>,selClosure:(index:Int)->Void,cancel:(()->Void)?) {
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController as?LDMainTabBarVC
        
        var alertVC:PCLBlurEffectAlert.Controller!
        
        
        //        兼容oc
        if title != nil {
            alertVC = PCLBlurEffectAlert.Controller(title: title,message: nil,style: .ActionSheet)
            //            alertVC.configure(titleLabHeight: 20)
        } else {
            alertVC = PCLBlurEffectAlert.Controller(title: nil,message: nil,style: .ActionSheet)
        }
        
        
        for i in 0..<labsTitleArr.count {
            
            let selAction = PCLBlurEffectAlert
                .AlertAction(title: labsTitleArr[i],style: .Default,handler: { action in
                    
                    selClosure(index: i)
                })
            
            alertVC.addAction(selAction)
            
        }
        
        let cancelAction = PCLBlurEffectAlert
            .AlertAction(title: "取消",style: .Cancel,handler: { action in
                
                guard cancel == nil else {
                    cancel!()
                    return
                }
                
            })
        
        alertVC.addAction(cancelAction)
        
        let heightStr = String(labHeight)
        let height = CGFloat(Int(heightStr) ?? 0)
        
        if height > 15 {
            alertVC.configure(buttonHeight: height)
        }
        
        //字体配置等
        configureActionVC(alertVC)
        
        NSOperationQueue.mainQueue().addOperationWithBlock { 
            
            target!.presentViewController(alertVC, animated: true, completion: nil)
        }
    }
    
    //action Sheet 选择列表样式
    func showLDActionSheetView(title:String?,labHeight:AnyObject?,
                               labsTitleArr:Array<String>,selClosure:(index:Int)->Void) {
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        
        var alertVC:PCLBlurEffectAlert.Controller!
        alertVC = PCLBlurEffectAlert.Controller(title: title,message: nil,style: .ActionSheet)
        
        
        //兼容oc
        if title != nil {
            alertVC = PCLBlurEffectAlert.Controller(title: title,message: nil,style: .ActionSheet)
            //            alertVC.configure(titleLabHeight: 20)
        }
        
        
        for i in 0..<labsTitleArr.count {
            
            let selAction = PCLBlurEffectAlert
                .AlertAction(title: labsTitleArr[i],style: .Default,handler: { action in
                    
                    selClosure(index: i)
                })
            
            alertVC.addAction(selAction)
        }
        
        
        //        let heightStr = String.init(format: "%d", labHeight as f)
        
        let height = labHeight as? CGFloat ?? 35
        //        let height = CGFloat(Int(heightStr) ?? 0)
        
        if height > 15 {
            alertVC.configure(buttonHeight: height)
        }
        
        //字体配置等
        alertVC.configure(alertViewWidth: LDFrame.ScreenWidth - 10)
        configureActionVC(alertVC)
        
        alertVC.view.userInteractionEnabled = true
        alertVC.view.bk_whenTapped {
            /**
             *  发送通知到发现页 以改变图标
             *
             */
            NSNotificationCenter.defaultCenter().postNotificationName(DISSMISSACTIONSHEET,
                object: self, userInfo:nil)
            
            alertVC.dismissViewControllerAnimated(true, completion: nil)
        }
        
        NSOperationQueue.mainQueue().addOperationWithBlock {
            
            target!.presentViewController(alertVC, animated: true, completion: nil)
        }
        
    }
    
    func configureActionVC(alertVC:PCLBlurEffectAlert.Controller) {
        
        //字体配置等
        alertVC.configure(cornerRadius: 3)
        alertVC.configure(titleFont: LDFont.Font15, textColor: LDFontColor.Green)
        alertVC.configure(messageFont: LDFont.Font13, textColor: LDFontColor.DeepGray)
        
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Default: LDFont.Font14], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Default: LDFontColor.DeepGray], buttonDisableTextColor: nil)
        
        alertVC.configure(backgroundColor: UIColor.whiteColor())
    }
    
    
    // 取消在左边(红色)    确定在右边(绿色)
    func ShowZTAlertView(info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        
        
        
        let alertVC = PCLBlurEffectAlert.Controller(title: info,
                                                    message: nil,
                                                    style: .Alert)
        let cancelAction = PCLBlurEffectAlert.AlertAction(title: "取消",
                                                          style: .Cancel,
                                                          handler: { action in
                                                            
                                                            if cancel != nil {
                                                                cancel!()
                                                            }
        })
        
        
        let okAction = PCLBlurEffectAlert.AlertAction(title: "确定",
                                                      style: .Destructive,
                                                      handler: { action in
                                                        
                                                        if sure != nil {
                                                            sure!()
                                                        }
                                                        
        })
        
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Cancel:LDFontColor.Red], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        alertVC.configure(buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        target!.presentViewController(alertVC, animated: true, completion: nil)
        
    }
    
    
    
    //确认 title message 样式
    func ShowZTTiteAlertView(title:String,info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        let alertVC = PCLBlurEffectAlert.Controller(title: title,
                                                    message: info,
                                                    style: .Alert)
        
        let cancelAction = PCLBlurEffectAlert.AlertAction(title: "稍后再说",
                                                          style: .Cancel,
                                                          handler: { action in
                                                            
                                                            if cancel != nil {
                                                                cancel!()
                                                            }
        })
        
        
        let okAction = PCLBlurEffectAlert.AlertAction(title: "立即更新",
                                                      style: .Destructive,
                                                      handler: { action in
                                                        
                                                        if sure != nil {
                                                            sure!()
                                                        }
                                                        
        })
        
        
        
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Cancel:LDFontColor.Red], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        alertVC.configure(buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        target!.presentViewController(alertVC, animated: true, completion: nil)
        
    }
    
    
    //确认 title message 样式  自定义
    func ShowZTCustomTiteAlertViewCancleTitle(cancleTitle:String,sureTitle:String,title:String,info:String,sure:(()->Void)?,cancel:(()->Void)?) {
        
        let alertVC = PCLBlurEffectAlert.Controller(title: title,
                                                    message: info,
                                                    style: .Alert)
        
        let cancelAction = PCLBlurEffectAlert.AlertAction(title: cancleTitle,
                                                          style: .Cancel,
                                                          handler: { action in
                                                            
                                                            if cancel != nil {
                                                                cancel!()
                                                            }
        })
        
        
        let okAction = PCLBlurEffectAlert.AlertAction(title: sureTitle,
                                                      style: .Destructive,
                                                      handler: { action in
                                                        
                                                        if sure != nil {
                                                            sure!()
                                                        }
                                                        
        })
        
        
        
        alertVC.addAction(cancelAction)
        alertVC.addAction(okAction)
        alertVC.configure(titleFont: LDFont.Font16, textColor: LDFontColor.Black)
        alertVC.configure(buttonFont: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFont.Font16], buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Cancel:LDFontColor.Red], buttonDisableTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        alertVC.configure(buttonTextColor: [PCLBlurEffectAlert.ActionStyle.Destructive:LDFontColor.Green])
        
        let target = UIApplication.sharedApplication().keyWindow?.rootViewController
        target!.presentViewController(alertVC, animated: true, completion: nil)
        
    }
    
}




class LDShowAlertOrSheet: NSObject {
    
    
    //    weakSelf.experiencesArray = [NSMutableArray arrayWithArray:experienceArray];
    //    NSMutableArray *titlLabsArr = [NSMutableArray array];
    //
    //    for (DDCommonDataModel *experience in weakSelf.experiencesArray) {
    //
    //    [titlLabsArr addObject:experience.experience_text];;
    //
    //    }
    //
    //    [self showLDActionSheetView:@"" target: self.tabBarController labHeight:35 labsTitleArr:titlLabsArr selClosure:^(NSInteger a) {
    //
    //    } cancel:^{
    //    
    //    }];
}
